module UnfuddleMyEmail
  class Runner
    def initialize(configuration_path)
      @options = Configuration.new(configuration_path)
    end

    def run
      fetcher = Fetcher.new(@options.pop3_server, @options.pop3_port, @options.pop3_ssl, @options.pop3_username, @options.pop3_password, @options.pop3_delete)
      fetcher.each do |message|
        ticket = EmailTicket.new(message)
        p "Posting ticket: #{ticket.message.subject}"
        Poster::post(domain, url, @options.unfuddle_ssl, @options.unfuddle_username, @options.unfuddle_password, ticket.to_xml)
      end
    end

    private

    def domain
      "#{@options.unfuddle_subdomain}.unfuddle.com"
    end

    def url
      "#{@options.unfuddle_api_url}/projects/#{@options.unfuddle_project_id}/tickets"
    end
  end
end
